const app = getApp()
import http from '../../../utils/http'
Page({

  data: {
    nameKey: "",
    nutrientContent: [],
    foodContent: [],
    addContent: false,
    contentAmount: 0,
    contentId: "",
    contentName: "",

    name: undefined,
    description: undefined,
    pictureUrl: "../../../static/images/add.jpg"
  },

  onLoad: function (options) {
    http({
      url: '/ingredient/adminPageOrder/1/20',
      method: "post",
      data: {
        nameKey: "小麦"
      },
      success: (result) => {
        console.log(result)
        this.setData({
          nutrientContent: result.rows
        })
      }
    })
  },

  // 获取图片路径
  locadPicture: function () {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        that.setData({
          pictureUrl: tempFilePaths[0]
        })
      }
    })
  },

  onShow: function () {

  },
  showAddContent(e) {
    console.log(e)
    this.setData({
      addContent: true,
      contentId: e.currentTarget.dataset.item.ncId,
      contentName: e.currentTarget.dataset.item.ncName,
    })
  },

  confirmAddContent() {
    var that = this;
    var temp = this.data.foodContent;
    temp.push({
      id: that.data.contentId,
      name: that.data.contentName,
      amount: that.data.contentAmount
    })
    this.setData({
      addContent: false,
      foodContent: temp
    })
  },

  candelAddContent() {
    this.setData({
      addContent: false
    })
  },

  removeContent(e) {
    var that = this;
    var temp = this.data.foodContent;
    temp.splice(e.currentTarget.dataset.index, 1)
    this.setData({
      foodContent: temp
    })
  },

  searchContent(e) {
    http({
      url: '/ingredient/adminPageOrder/1/5',
      method: "post",
      data: {
        nameKey: e.detail.value
      },
      success: (result) => {
        console.log(result)
        this.setData({
          nutrientContent: result.rows
        })
      }
    })
  },

  addFood() {
    if (this.data.name == undefined) {
      wx.showModal({
          title: '提示',
          content: '请输入食谱名称',
      })
      return
    }
    if (this.data.description == undefined) {
      wx.showModal({
          title: '提示',
          content: '请输入食谱简介',
      })
      return
    }
    var that = this
    // 图片上传
    if (that.data.pictureUrl == "../../img/add.jpg") {

    } else {
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.uploadFile({
        url: 'http://localhost:8081/api/oss',
        filePath: that.data.pictureUrl,
        name: 'file',
        header: {
          'content-type': 'application/json',
          'Cookie': 'JSESSIONID=' + app.globalData.sessionId
        },
        formData: {
          'user': 'test'
        },
        success: function (res) {
          wx.hideLoading();
          console.log(url)
          let url = JSON.parse(res.data).data.url
          http({
            url: '/food/add',
            method: "post",
            data: {
              name: that.data.name,
              picture: url,
              description: that.data.description,
              contentList: that.data.foodContent,
            },
            success: (result) => {
              console.log(result)
              wx.showModal({
                title: '提示',
                content: '添加成功！',
                success(res) {
                  if (res.confirm) {
                    wx.switchTab({
                      url: '../../food/food.js'
                    });
                  } else if (res.cancel) {

                  }
                }
              })
            }
          })
        },
        fail: function (res) {
          wx.hideLoading();
          wx.showModal({
            title: '提示',
            content: '操作失败，请稍后重试',
          })
        }
      })
    }
  },
  

  /**
   * 跳转方法
   */
  toIngredientDetail(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/ingredient_inner/detail/detail?id='+e.currentTarget.dataset.id
    })
  },

  /**
   * 其他无用方法
   */
  bindinput() {},
})